package com.cnhis.cloudhealth.clinical.infusion.base.bo;

import com.cnhis.cloudhealth.clinical.infusion.base.dao.CliSysParametersDao;
import com.cnhis.cloudhealth.clinical.infusion.base.dao.MobileInfusionDao;
import com.cnhis.cloudhealth.clinical.infusion.base.mapper.SysParameters;
import com.cnhis.cloudhealth.commons.cardmember.bo.CardMemberMangerBo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hdd on 2016-11-02.
 */
@Service
public class MobileInfusionBo {
    @Autowired
    private MobileInfusionDao mobileInfusionDao;
    @Autowired
    private GetNewId getNewId;
    @Autowired
    private CliSysParametersDao sysParametersDao;
    @Autowired
    private CardMemberMangerBo cardMemberMangerBo;
    /**
     * 查询病人列表信息 （输液待执行病人）
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> query1476p(Map<String, Object> map) throws Exception{
        return mobileInfusionDao.query1476p(map);
    }

    /**
     * 查询病人列表信息 （输液 已完成  病人）
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> findCompletedPatientList(Map<String, Object> map) throws Exception{
        return mobileInfusionDao.findCompletedPatientList(map);
    }

    /**
     * 查询病区科室
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDept() throws Exception{
        return mobileInfusionDao.queryDept();
    }

    /**
     * 根据 就诊id  查询此病人的详细信息
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDetailPatient(Map<String, Object> map) throws Exception{
        return mobileInfusionDao.queryDetailPatient(map);
    }

    /**
     * 获取病人  执行项目
     * 待执行病人  （进行中的 项目）
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryExecutePro(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryExecutePro(map, currPage, pageSize);
    }

    /**
     * 获取病人  执行项目
     * 已完成  病人  （已完成的 项目）
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryCompleteExecutePro(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryCompleteExecutePro(map, currPage, pageSize);
    }

    /**
     * 获取病人  执行记录   也就是项目的明细
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryExecuteRecord(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryExecuteRecord(map, currPage, pageSize);
    }

    /**
     * 获取收费 记录
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryChargeRecord(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryChargeRecord(map, currPage, pageSize);
    }

    /**
     * 查询所有  待执行医嘱  信息
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryExcuseDoctor(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryExcuseDoctor(map, currPage, pageSize);
    }


    /**
     * 接单时  查询搜索的人员
     * 执行人  配药人  接单人
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryUser(Map<String, Object> paraMap)throws Exception{
        return mobileInfusionDao.queryUser(paraMap);
    }

    /**
     * 保存接单 信息
     * member//会员类型   金卡2，普通卡1，钻石卡3
     * @return
     * @throws Exception
     */
    public String saveOrders(Map<String, Object> vbj1_map ,/**病人医嘱执行  基本信息**/
                             List<Map<String, Object>> vaf1_list,  /**病人医嘱记录  list表格**/
                             Map<String, Object> userMap, String vaa01, int memeber, String vaa07
    )throws Exception{
        String msg = "";
        //根据 vaf_list 的值 查询视图 V_VAF_1
        String flag = searchVafView(vaf1_list);

        //判断病人卡上钱是否够用   暂时不用  这个适用于 会员卡
        if(memeber == 1 || memeber == 2){
            //如果是会员 先去判断 选中的单据 是否已经交费了，如果交费了，则不用走会员扣费，直接可以进行接单操作
            Map<String,Object> baclistmap = mobileInfusionDao.searchChargeCount(vaf1_list);
            List accountlist = (List)baclistmap.get("list");
            if(accountlist.size()>0 && accountlist != null){
                //说明有未结账的单据
                Map<String,Object> vai23map = queryVai23(vbj1_map,vaf1_list);
                if(vai23map != null){
                    List list = (List)vai23map.get("list");
                    if(list != null && list.size()>0){
                        for (int i=0;i<list.size();i++){
                            Map<String,Object> listmap = (Map)list.get(i);
                            Float vai23 = Float.parseFloat(listmap.get("fvai23").toString());
                            Long vai01 = GlobalVariable.LongIsNull(listmap.get("vai01"));
                            if(vai23>0){
                                //查询看卡上钱 是否够用
                                Map<String,Object> vbuMap = mobileInfusionDao.queryCountByVaa01(vaa01);
                                Float sumCount = 0.0f;
                                if(vbuMap != null){
                                    String vbu14 = vbuMap.get("vbu14").toString();
                                    String bep06b = vbuMap.get("bep06b").toString();
                                    sumCount = Float.parseFloat(vbu14)+Float.parseFloat(bep06b);
                                }
                                if(sumCount<vai23){
                                    //说明钱不够用  给一个提示 说明    病人卡上余额不够，请先到收费处交钱
                                    msg="病人卡上余额不够，请先到收费处交钱";
                                    return msg;
                                }else{
                                    //进行扣费的操作  调用总的存储过程
                                    //Map<String,Object> vaiAllMap = queryVaiAll(vbj1_map,vaf1_list);
                                    Map<String,Object> cardMap = new HashMap<String,Object>();
                                    cardMap.put("VAA01",vaa01);
                                    cardMap.put("VAA07",vaa07);
                                    cardMap.put("VAI01", vai01);
                                    cardMap.put("ACF01",1);
                                    cardMap.put("BCE01",userMap.get("bce01").toString());
                                    cardMap.put("BCk01",userMap.get("depId").toString());
                                    cardMap.put("BCE02",userMap.get("bce02c").toString());
                                    cardMap.put("BCE03",userMap.get("bce03c").toString());
                                    cardMap.put("VAJ38",vai23);
                                    cardMap.put("AType",0);
                                    cardMap.put("AWorkType",4);
                                    //调用一卡通扣费的总方法
                                    com.cnhis.cloudhealth.commons.Mappers.ModelVo mv = new com.cnhis.cloudhealth.commons.Mappers.ModelVo();
                                    mv = cardMemberMangerBo.updatehoratecardcharge(cardMap);
                                    if (StaticKeys.OH_FALSE.equals(mv.getResult())) {
                                        msg = mv.getResultMsg();
                                        return msg;
                                    }
                                }
                            }
                        }
                    }

                }
            }else{
                //说明都已经结账了  则不用调用会员扣费方法
            }
        }else{
            //先去判断 病人的费用是否已经收了，如果收了 则走下面，如果没收，则给提示
            Map<String,Object> baclistmap = mobileInfusionDao.searchChargeCount(vaf1_list);
            List accountlist = (List)baclistmap.get("list");
            if(accountlist.size()>0 && accountlist != null){
                msg ="还有未交费的单据,请先到收费处交费";
                return msg;
            }else{
                //说明都已经交费了
            }
        }

        if(flag.equals("no")){  //  说明没有 值 小于8   还没执行或已经作废，不能执行
            saveVbj1(vbj1_map,vaf1_list);
            saveVbi(vaf1_list);//修改医嘱状态
            saveVaf1(vaf1_list,userMap);
        }
        msg = "执行成功";
        return msg;
    }

    public Map<String,Object> queryVai23(Map<String, Object> vbj1_map,List<Map<String, Object>> vaf1_list)throws Exception{
        vbj1_map.put("list",vaf1_list);
        return mobileInfusionDao.queryVai23(vbj1_map);
    }

    public Map<String,Object> queryVaiAll(Map<String, Object> vbj1_map,List<Map<String, Object>> vaf1_list)throws Exception{
        vbj1_map.put("list",vaf1_list);
        return mobileInfusionDao.queryVaiAll(vbj1_map);
    }



    /**
     * 根据 vaf_list 的值 查询视图 V_VAF_1
     * 循环 判断  vaf10 是否有小于8 的
     */
    public String searchVafView(List<Map<String, Object>> vaf1_list)throws Exception{
        List<Map<String,Object>> list = mobileInfusionDao.searchVafView(vaf1_list);
        String flag = "no";//说明没有 值 小于8
        if(list.size()>0 && list != null){
            flag = "yes";//说明查询出来有小于 8 的 数据
        }
        return flag;
    }

    /**
     * 保存医嘱 执行 基本信息
     * @param vbj1_map
     * @throws Exception
     */
    public void saveVbj1(Map<String, Object> vbj1_map,List<Map<String, Object>> vaf1_list)throws Exception {
        vbj1_map.put("list",vaf1_list);
        vbj1_map.put("id",getNewId.nextId());
        mobileInfusionDao.saveVbj1(vbj1_map);
    }

    /**
     * 修改医嘱状态
     * @param vaf1_list
     * @throws Exception
     */
    public void saveVbi(List<Map<String, Object>> vaf1_list)throws Exception{
        Map<String,Object> paraMap = new HashMap<String,Object>();
        paraMap.put("productid",100);
        paraMap.put("programid",107001);
        paraMap.put("paramno",1);
        SysParameters parameters =sysParametersDao.queryParameterByPara(paraMap);
        String param3 = "0";
        if(!"".equals(parameters) && parameters != null){
            param3 = parameters.getValue();
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",vaf1_list);
        map.put("param3",param3);
        mobileInfusionDao.updateVBI1(map);
        mobileInfusionDao.updateVBI1_2(map);
    }

    /**
     * 保存医嘱 记录  到 vaf1表
     * @param vaf1_list
     * @throws Exception
     */
    public void saveVaf1(List<Map<String, Object>> vaf1_list ,Map<String, Object> userMap/**病人医嘱记录**/) throws Exception {
        if (vaf1_list != null && vaf1_list.size() > 0) {
            //修改
            userMap.put("list",vaf1_list);
            mobileInfusionDao.batchUpdateByVaf(userMap);
        }
    }

    /**
     *  查询  输液单，注射单 等 各个单据  用于打印
     * @return
     * @throws Exception
     */
    public Map<String,Object> querySingleinfusion(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.querySingleinfusion(paramMap);
    }

    /**
     * 点击执行完成 的操作
     * @return
     * @throws Exception
     */
    public String performOver(List<Map<String, Object>> cbmAndVaf_list)throws Exception{
        return mobileInfusionDao.performOver(cbmAndVaf_list);
    }

    /**
     * 退费审核
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryRefundAudit(Map<String, Object> paramMap,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryRefundAudit(paramMap, currPage, pageSize);
    }

    /**
     * 退费审核中的 人员搜索
     * @param paramMap
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryUserInRefundAudit(Map<String, Object> paramMap,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.queryUserInRefundAudit(paramMap, currPage, pageSize);
    }

    /**
     * 根据科室查询  此科室下的所有人员
     * 退费审核
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String,Object> refundAuditByDepId(Map<String, Object> paramMap,int currPage, int pageSize)throws Exception{
        return mobileInfusionDao.refundAuditByDepId(paramMap, currPage, pageSize);
    }


    /**
     * 点击皮试结果
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String,Object> skinTest(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.skinTest(paramMap);
    }

    /**
     * 基本信息 保存皮试前使用
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryBaseInfo(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.queryBaseInfo(paramMap);
    }

    /**
     * 皮试的修改
     * 保存皮试信息   更新vaf
     * @param paramMap
     * @throws Exception
     */
    public void saveSkinTestResult(Map<String, Object> paramMap)throws Exception{
        mobileInfusionDao.saveSkinTestResult(paramMap);
    }

    /**
     * 查询是否具有过敏记录
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinRecord(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.queryIsHasSkinRecord(paramMap);
    }

    /**
     * 添加过敏记录
     * @param paramMap
     * @throws Exception
     */
    public void addSkinRecord(Map<String, Object> paramMap)throws Exception{
        mobileInfusionDao.addSkinRecord(paramMap);
    }

    /**
     * 修改过敏记录
     * @param paramMap
     * @throws Exception
     */
    public void updateSkinRecord(Map<String, Object> paramMap)throws Exception{
        mobileInfusionDao.updateSkinRecord(paramMap);
    }

    /**
     * 查询是否具有过敏药物
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinDrug(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.queryIsHasSkinDrug(paramMap);
    }

    /**
     * 添加过敏药物
     * @param paramMap
     * @throws Exception
     */
    public void addSkinDrug(Map<String, Object> paramMap)throws Exception{
        mobileInfusionDao.addSkinDrug(paramMap);
    }

    /**
     * 退费审核的保存
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> refund(Map<String, Object> map) throws Exception {
        Map<String, Object> msg=new HashMap<String,Object>();
        /**这里需要扣费HORate_CardCharge_Update**/
        for (Map<String,Object> m:(List<Map<String,Object>>)map.get("list")) {
            Map<String,Object> backMap=mobileInfusionDao.findAppRefund(m);
            if(backMap!=null){
                msg.put("falg",false);
                msg.put("msg","费用ID:"+m.get("VAJ01").toString()+"已经审核或拒绝退费，请返回刷新重新提交.");
                return msg;
            }else{
                m.put("BCE01B",map.get("BCE01B"));
                m.put("BCE03B",map.get("BCE03B"));
                m.put("lDate",map.get("lDate"));
                mobileInfusionDao.refund1(m);
                mobileInfusionDao.refund2(m);
            }
        }
        msg.put("falg",true);
        msg.put("msg","审核成功");
        return msg;
    }

    /**
     * 根据 vaf01  查询vbj表  查询出  执行时间 vaj08  用于打印
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryVaj08ByVaf01(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.queryVaj08ByVaf01(paramMap);
    }

    /**
     * 查询病人 是不是会员
     * @throws Exception
     */
    public Map<String,Object> findMembers(String vaa01)throws Exception{
        return mobileInfusionDao.findMembers(vaa01);
    }

    /**
     * 查询病人 会员卡类型
     */
    public Map<String,Object> findMemberCardType(String vaa01)throws Exception{
        return mobileInfusionDao.findMemberCardType(vaa01);
    }

    /**
     * 根据 科室 和  vaf01 查询是否可以申请  是本科室给药途径退费
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryApplyRefuse(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.queryApplyRefuse(paramMap);
    }

    /**
     * 配液执行完成的操作
     * @param paramMap
     * @return
     * @throws Exception
     */
    public String technicExecToComplete(Map<String, Object> paramMap)throws Exception{



        return mobileInfusionDao.technicExecToComplete(paramMap);
    }

    /**
     * 确认执行穿刺
     * @param paramMap
     * @return
     * @throws Exception
     */
    public String ohmobilesyworkinsert(Map<String, Object> paramMap)throws Exception{
        return mobileInfusionDao.ohmobilesyworkinsert(paramMap);
    }

    /**
     * 判断是输液是否已经开始了  Mobile_SY_WORK
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List checkInfusionStart(Map<String,Object> paramMap)throws Exception{
        return mobileInfusionDao.checkInfusionStart(paramMap);
    }

    /**
     * 查询 oldvbi21  是否正在输液
     * @param paramMap
     * @return
     * @throws Exception
     */
    public String queryoldVBI21(Map<String,Object> paramMap) throws Exception{
        return mobileInfusionDao.queryoldVBI21(paramMap);
    }

    /**
     * 判断是否已经输液完成
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List checkInfusionEnd(Map<String,Object> paramMap)throws Exception{
        return mobileInfusionDao.checkInfusionEnd(paramMap);
    }

    /**
     * 不换瓶子 的保存方法
     * @param paramMap
     * @throws Exception
     */
    public void insertSywFlg0(Map<String,Object> paramMap)throws Exception{
        mobileInfusionDao.insertSywFlg0(paramMap);
    }




}
